package com.itheima.tanhua.apiImpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.tanhua.api.mongodb.UserLikeApi;
import com.itheima.tanhua.pojo.mongo.UserLike;
import com.itheima.tanhua.vo.UsersCountsVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存
    @Override
    public boolean saveLove(Long id, Long uid, String islike) {
        //首先查询数据,是否存在
        Query query=Query.query(Criteria.where("userId").is(uid)
                .and("likeUserId").is(id).and("isLike").is(islike));
        boolean flag = mongoTemplate.exists(query, UserLike.class);
        if (flag){
            throw new RuntimeException("您已经添加了此用户的喜欢数据");
        }
        UserLike userLike=new UserLike();
        userLike.setIsLike(islike);
        userLike.setUserId(uid);
        userLike.setLikeUserId(id);
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUpdated(System.currentTimeMillis());

        UserLike save = mongoTemplate.save(userLike);
       return ObjectUtil.isEmpty(save)? false: true;
    }

    @Override
    public boolean isLike(Long id, Long uid, String islike) {
        Query query=Query.query(Criteria.where("userId").is(id)
                .and("likeUserId").is(uid).and("isLike").is(islike));
        return mongoTemplate.exists(query, UserLike.class);
    }

    //互相喜欢，喜欢，粉丝 - 统计
    public UsersCountsVo counts(Long uid) {
        //互相喜欢 查询用户喜欢的列表 两用户相互喜欢同时满足
        //喜欢 以当前用户的uid去查询用户的喜欢的数量
        //粉丝 以当前用户的uid当作likeuserId 查询用户的数量
        UsersCountsVo vo =new UsersCountsVo();
        //获取喜欢的数据
        Query query = Query.query(Criteria.where("userId").is(uid));
        List<UserLike> likes1 = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserId1 = CollUtil.getFieldValues(likes1, "likeUserId", Long.class);
        vo.setLoveCount(likes1.size());
        //粉丝的数据
        Query query1 = Query.query(Criteria.where("likeUserId").is(uid));
        List<UserLike> likes2 = mongoTemplate.find(query1, UserLike.class);
        List<Long> likeUserId2 = CollUtil.getFieldValues(likes2, "UserId", Long.class);
        vo.setFanCount(likes2.size());
        //互相喜欢的数据
        List<Long> list=new ArrayList<>();
        list.addAll(likeUserId1);
        list.addAll(likeUserId2);
        //去重 使用set集合
        List<Long> newList=new ArrayList<>(new HashSet<>(list));
        vo.setEachLoveCount(newList.size());
        return vo;
    }
//喜欢列表
    @Override
    public List<Long> getLove(Long uid) {
        Query query=Query.query(Criteria.where("userId").is(uid));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
    }
}
